package com.adtec.base.cloud.comminicate.communicate.file.controller;

import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.commons.exception.CustomExceptionType;
import com.adtec.base.cloud.commons.utils.FileTool;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@RequestMapping("/upload")
@Slf4j
public class FileController {
    @PostMapping(value = "/uploadFile",consumes = "multipart/form-data")
    @Operation(description = "上传")
    public AjaxResponse initCube(@RequestPart("file") MultipartFile multipartfile) {
        if (!multipartfile.isEmpty()) {
            FileTool fileTool = new FileTool();
            File oLocalFile = fileTool.conversionType(multipartfile);
            return AjaxResponse.success("文件 " + oLocalFile.getName() + " 上传成功!所在路径: " + oLocalFile.getAbsolutePath());
        } else {
            return AjaxResponse.error(CustomExceptionType.USER_INPUT_ERROR, "文件不能为空");
        }
    }
}
